﻿using System;
using System.Collections.Generic;
using Anderson.PluginFramework;
using UnityEngine;

public class UseRotateServicePlugin : MonoBehaviour, IPluginActivator, IFrameworkListener, IPluginListener, IServiceListener
{

    IRotateService serviceInstance = null;
    GameObject container = null;
    bool found = false;
    Type rotateServiceType = null;

    public void FrameworkChanged(FrameworkEventArg arg)
    {
        plugin.Log.Debug(string.Format("UseRotateServicePlugin - FrameworkChanged : {0}", arg.State));

        if (arg.State == FrameworkEventType.AllPluginsLoaded)
        {            
            IServiceBus bus = framework;
            IDictionary<string, ServiceReference> dict =  bus.DictAllService;
            foreach (KeyValuePair<string, ServiceReference> item in dict)
            {
                Type t = item.Value.serviceType;
                if (typeof(IRotateService).IsAssignableFrom(t))
                {
                    found = true;
                    //该插件知道IRotateService的实现类是继承自MonoBehaviour
                    rotateServiceType = t;

                    plugin.Log.Debug("UseRotateServicePlugin - 找到IRotateService的实现 ： " + rotateServiceType.Name);

                    break;
                }
            }

            if (plugin.Started)
            {
                DoRun();
            }
        }
    }

    public void PluginChanged(PluginEventArg arg)
    {
        plugin.Log.Debug(string.Format("UseRotateServicePlugin - PluginChanged {0} : {1}", arg.Plugin.Entity.pluginName, arg.State));
    }

    public void ServiceChanged(ServiceEventArg arg)
    {
        plugin.Log.Debug(string.Format("UseRotateServicePlugin - ServiceChanged {0} : {1}", arg.Plugin.Entity.pluginName, arg.State));
    }

    #region IPluginActivator
    private IPlugin plugin;
    private IFramework framework;

    public void Init(IFramework framework, IPlugin plugin)
    {
        this.framework = framework;
        this.plugin = plugin;

        plugin.Log.Debug("UseRotateServicePlugin.Init");
    }

    public void Run()
    {        
        plugin.Log.Debug("UseRotateServicePlugin.Start");

        DoRun();
    }

    public void Stop()
    {
        plugin.Log.Debug("UseRotateServicePlugin.Stop");

        if (null != serviceInstance)
        {
            Destroy((UnityEngine.Object)serviceInstance);

            serviceInstance = null;
        }
    }
    #endregion

    void DoRun()
    {
        plugin.Log.Debug("UseRotateServicePlugin.DoRun 1");

        if (null != serviceInstance)
        {
            return;
        }

        plugin.Log.Debug("UseRotateServicePlugin.DoRun 2");

        if (found && plugin.IsGameObjectActivator)
        {
            plugin.Log.Debug("UseRotateServicePlugin.DoRun 3");
            //MonoBehaviour component = (MonoBehaviour)plugin.ActivatorInstance;
            //GameObject container = component.gameObject;

            //GameObject container = framework.PluginRootTransform.gameObject;

            IRotateApp app = (IRotateApp)framework;
            container = app.RotateObject.gameObject;

            serviceInstance = (IRotateService)container.AddComponent(rotateServiceType);
            serviceInstance.Speed = 50;
        }
        else
        {
            plugin.Log.Debug("UseRotateServicePlugin - 未找到IRotateService的实现。");
        }
    }
}
